package com.zlf.po;

import com.zlf.util.WalletRules;
import lombok.Data;

import javax.naming.InsufficientResourcesException;
import java.math.BigDecimal;

public class Wallet {
    private Long id;
    private String payPassword;
    private Long createTime;
    private BigDecimal balance=BigDecimal.ZERO;
    private boolean isAllowedOverdraft=false;
    private BigDecimal overdraftAmount=BigDecimal.ZERO;
    private BigDecimal frozenAmount=BigDecimal.ZERO;
    public Wallet(Long preAllocatedId,String payPassword){
        this.id=preAllocatedId;
        this.payPassword=payPassword;
        this.createTime= System.currentTimeMillis();
    }
    public Long id(){return id;}
    public BigDecimal overdraftAmount(){return this.overdraftAmount;}
    public boolean isOverdraft()
    {
        return (this.balance.compareTo(BigDecimal.ZERO)<0);
    }
    public boolean checkPass(String pass){
        return pass.equals(payPassword);
    }
    public void freeze(BigDecimal amount) throws Exception{
        if(amount.compareTo(BigDecimal.ZERO)<0){
            throw new InsufficientResourcesException("传入金额有误");
        }
        this.frozenAmount=this.frozenAmount.add(amount);
    }
    public void unfreeze(BigDecimal amount) throws Exception{
        if((amount.compareTo(BigDecimal.ZERO)<0)||(this.frozenAmount.compareTo(amount)<0)){
            throw new InsufficientResourcesException("传入金额有误");
        }
        this.frozenAmount=this.frozenAmount.subtract(amount);

    }
    public void increaseOverdraftAmount(BigDecimal amount) throws Exception{
        if(amount.compareTo(BigDecimal.ZERO)<0){
            throw new InsufficientResourcesException("传入金额有误");
        }
        this.overdraftAmount=this.overdraftAmount.add(amount);
    }
    public void decreaseOverdraftAmount(BigDecimal amount)throws Exception{
        if((amount.compareTo(BigDecimal.ZERO)<0)||(this.overdraftAmount.compareTo(amount)<0)){
            throw new InsufficientResourcesException("传入金额有误");
        }
        this.overdraftAmount=this.overdraftAmount.subtract(amount);
    }
    public void closeOverdraft()throws Exception{
        if(this.balance.subtract(this.frozenAmount).compareTo(BigDecimal.ZERO)<0){
            throw new InsufficientResourcesException("欠款尚未还清");        }
        else {
            this.isAllowedOverdraft=false;
        }
    }
    public void openOverdraft(){
        this.isAllowedOverdraft=true;
    }

    public BigDecimal balance(){
        return this.balance;
    }

    public BigDecimal getAvailableBalance(){
        BigDecimal totalAvailableBalance=this.balance.subtract(this.frozenAmount);
        if(isAllowedOverdraft)
        {
            totalAvailableBalance=totalAvailableBalance.add(this.overdraftAmount);
        }
        return totalAvailableBalance;
    }

    //出
    public void debit(BigDecimal amount) throws Exception{
        BigDecimal totalAvailableBalance=getAvailableBalance();
        if((amount.compareTo(BigDecimal.ZERO)<0)||(totalAvailableBalance.compareTo(amount)<0)){
            throw new InsufficientResourcesException("余额不足");
        }
        this.balance=this.balance.subtract(amount);
    }

    //入
    public void credit(BigDecimal amount) throws Exception{
        if(amount.compareTo(BigDecimal.ZERO)<0){
            throw new InsufficientResourcesException("传入金额有误");
        }
        this.balance=this.balance.add(amount);
    }

    public void interest(){
        if(this.isOverdraft()){
            this.balance=this.balance.add(this.balance.multiply(WalletRules.interestRate));
        }
    }
}
